#include "lrcpage.h"
#include "ui_lrcpage.h"

#include <QMouseEvent>
#include <QPropertyAnimation>
#include <QJsonArray>
#include <QFile>
#include <QJsonObject>
#include <QJsonValue>

LrcPage::LrcPage(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::LrcPage)
{
    ui->setupUi(this);

    setWindowFlag(Qt::FramelessWindowHint);

    pullDownAnimation = new QPropertyAnimation(this, "geometry", this);
    pullDownAnimation->setDuration(250);
    pullDownAnimation->setStartValue(QRect(10, 10, width(), height()));
    pullDownAnimation->setEndValue(QRect(10, 10 + height(), width(), height()));

    //下拉按钮被点击
    connect(ui->hideBtn, &QPushButton::clicked, this, [=](){
        pullDownAnimation->start();
    });

    //下拉动画播放结束
    connect(pullDownAnimation, &QPropertyAnimation::finished, this, [=](){
        hide();
    });
}

void LrcPage::parseLrcFile(QString &filePath)
{
    lrcLines.clear();

    QFile* lrcFile = new QFile(filePath);
    lrcFile->open(QIODevice::ReadOnly);

    if(!lrcFile->isOpen())
    {
        qDebug() << "文件打开失败----";
        return;
    }
    else
    {
        QString line = lrcFile->readLine();
        QString minute, second, miniSecond;
        while(!line.isEmpty())
        {
            LrcLineInfo lrcLineInfo;

            //先解析歌词
            int pos = line.indexOf("]");
            lrcLineInfo.lryrics = line.mid(pos + 1, line.size() - 2 - pos);

            int start = 1, end = 0;
            //先解析分钟
            end = line.indexOf(":", start); //end = 3
            minute = line.mid(start, end - start);
            start = end + 1;

            //在解析秒
            end = line.indexOf(".", start);
            second = line.mid(start, end - start);
            start = end + 1;

            //再解析毫秒
            end = line.indexOf("]", end);
            miniSecond = line.mid(start, end - start);

            //解析出总时长
            lrcLineInfo.time = minute.toInt() * 60000 + second.toInt() * 1000 + miniSecond.toInt();

            lrcLines.push_back(lrcLineInfo);

            //再次读取
            line = lrcFile->readLine();
        }
    }
}

void LrcPage::showLrcWordLine(int position)
{
    int index = getLrcLineIndex(position);
    ui->lineCenter->setStyleSheet("color: #1ecc94");
    if(index == -1)
    {
        ui->line1->setText("");
        ui->line2->setText("");
        ui->line3->setText("");
        ui->lineCenter->setText("当前歌曲暂无歌词");
        ui->line4->setText("");
        ui->line5->setText("");
        ui->line6->setText("");
    }
    else
    {
        ui->line1->setText(getLrcLineWord(index - 3));
        ui->line2->setText(getLrcLineWord(index - 2));
        ui->line3->setText(getLrcLineWord(index - 1));
        ui->lineCenter->setText(getLrcLineWord(index));
        ui->line4->setText(getLrcLineWord(index + 1));
        ui->line5->setText(getLrcLineWord(index + 2));
        ui->line6->setText(getLrcLineWord(index + 3));
    }
}

int LrcPage::getLrcLineIndex(int position)
{
    if(lrcLines.isEmpty())
    {
        return -1;
    }

    if(position < lrcLines[0].time)
    {
        return 0;
    }

    for(int i = 1; i < lrcLines.size(); i++)
    {
        if(position > lrcLines[i - 1].time
            && position <= lrcLines[i].time)
        {
            return i - 1;
        }
    }

    return lrcLines.size() - 1;;
}

QString LrcPage::getLrcLineWord(int index)
{
    if(index < 0 || index >= lrcLines.size())
    {
        return "";
    }
    else
    {
        return lrcLines[index].lryrics;
    }
}

void LrcPage::setMusicInfo(QString& musicName, QString& musicSinger)
{
    ui->musicSinger->setText(musicName);
    ui->musicName->setText(musicSinger);
}

LrcPage::~LrcPage()
{
    delete ui;
}
